package com.atguigu.guli.service.trade.controller.api;


import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.helper.JwtInfo;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-11-13
 */
@RestController
@RequestMapping("api/trade/order")
public class ApiOrderController {
    @Autowired
    OrderService orderService;


    //查询用户是否购买课程
    @GetMapping("auth/getCourseIsBuy/{courseId}")
    public R getCourseIsBuy(HttpServletRequest request,@PathVariable String courseId){
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        String id = jwtInfo.getId();
        LambdaUpdateWrapper<Order> querywrapper = new LambdaUpdateWrapper<>();
        querywrapper.eq(Order::getCourseId,courseId);
        querywrapper.eq(Order::getMemberId,id);
        Order order = orderService.getOne(querywrapper);
        if (order==null){
            return R.ok().data("item","1");
        }else {
            return R.ok().data("item","0");
        }
    }

    //查询支付状态的接口
    @GetMapping("getOrderStatus/{orderId}")
    public R getOrderStatus(@PathVariable String orderId){
        Order order = orderService.getById(orderId);
        return R.ok().data("status",order.getStatus()+"");
    }

    //根据订单id查询订单信息
    @GetMapping("auth/getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        Order order = orderService.getById(orderId);
        return R.ok().data("item",order);
    }

    //创建订单
    @PostMapping("auth/createOrder/{courseId}")
    public R createOrder(HttpServletRequest request, @PathVariable String courseId){
        JwtInfo member = JwtHelper.getJwtInfo(request);
      String orderId =   orderService.createOrder(member.getId(),courseId);
      return R.ok().data("orderId",orderId);
    }
}

